package cn.lhl.web.cargo;

import cn.lhl.common.utils.UploadUtil;
import cn.lhl.domain.cargo.*;
import cn.lhl.service.cargo.ContractProductService;
import cn.lhl.service.cargo.ContractService;
import cn.lhl.service.cargo.ExtCproductService;
import cn.lhl.service.cargo.FactoryService;
import cn.lhl.web.controller.BaseController;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
    @Reference
    private ContractService contractService;

    @Reference
    private ContractProductService contractProductService;

    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    @RequestMapping(value = "/list", name = "分页查询所有")
    public String list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int size,
                       String contractId, String contractProductId) {
        //将接收的参数放入model中
        model.addAttribute("contractId", contractId);
        model.addAttribute("contractProductId", contractProductId);

        //需要所有厂家列表
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria feCriteria = factoryExample.createCriteria();
        feCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        model.addAttribute("factoryList", factoryList);

        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria excriteria = extCproductExample.createCriteria();
        excriteria.andContractProductIdEqualTo(contractProductId);
        //查出所有附件
        PageInfo pageInfo = extCproductService.findAll(extCproductExample, page, size);
        //将附件信息传入域对象中
        model.addAttribute("page", pageInfo);
        return "/cargo/extc/extc-list";
    }

    @RequestMapping(value = "/edit", name = "新增或修改")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws IOException {
        //字段赋值
        extCproduct.setCompanyId(companyId);
        extCproduct.setCompanyName(companyName);
        //计算附件总金额给字段赋值
        double amount = 0.0d;
        if (extCproduct.getCnumber() != null && extCproduct.getPrice() != null) {
            amount = extCproduct.getCnumber() * extCproduct.getPrice();
        }
        extCproduct.setAmount(amount);
        //根据是否有id判断看是新增还是修改
        if (StringUtils.isEmpty(extCproduct.getId())) {
            //新增附件
            //给id赋值
            extCproduct.setId(UUID.randomUUID().toString());
            //图片
            if (productPhoto != null) {
                String upload = new UploadUtil().upload(productPhoto.getBytes());
                extCproduct.setProductImage(upload);
            }
            //调用service数据库中新增附件
            extCproductService.save(extCproduct);
            //新增合同的总数量|总金额及附件数量|总金额
            //获得原先的合同实体
            String contractId = extCproduct.getContractId();
            Contract oldContract = contractService.findById(contractId);
            oldContract.setExtNum(oldContract.getExtNum() + 1);
            oldContract.setTotalAmount(oldContract.getTotalAmount() + extCproduct.getPrice() * extCproduct.getCnumber());
            //更新数据库中数据
            contractService.update(oldContract);
        } else {
            //修改附件信息
            //图片
            if (productPhoto != null) {
                String upload = new UploadUtil().upload(productPhoto.getBytes());
                extCproduct.setProductImage(upload);
            }
            extCproductService.update(extCproduct);
            //更新合同中总数量及总钱数
            //合同总金额减去当前附件总金额加上传入的附件金额(数量同理)
            //获得数据库中当前的合同实体信息
            String contractId = extCproduct.getContractId();
            Contract oldContract = contractService.findById(contractId);
            //得出数据库中附件实体信息
            ExtCproduct oldExtCproduct = extCproductService.findById(extCproduct.getId());
            oldContract.setTotalAmount(oldContract.getTotalAmount() - oldExtCproduct.getAmount() + extCproduct.getPrice() * extCproduct.getCnumber());
            oldContract.setExtNum(oldContract.getExtNum() - oldExtCproduct.getCnumber() + extCproduct.getCnumber());
            //更新数据库
            contractService.update(oldContract);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId=" +
                extCproduct.getContractId() + "&contractProductId=" + extCproduct.getContractProductId();
    }

    @RequestMapping(value = "/toUpdate", name = "去修改页面")
    public String toUpdate(String id, String contractId, String contractProductId) {
        //确认修改的是哪个合同下的哪个货物下的附件
        //根据id将附件实体类查询的出,放入extCproduct域对象中
        ExtCproduct extCproduct = extCproductService.findById(id);
        model.addAttribute("extCproduct", extCproduct);

        //将合同id及货物id放入model域中
        model.addAttribute("contractId", contractId);
        model.addAttribute("contractProductId", contractProductId);

        //需要得到所有附件厂家信息
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        model.addAttribute("factoryList", factoryList);

        return "/cargo/extc/extc-update";
    }

    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id, String contractId, String contractProductId) {
        //根据id删除并更新合同中的信息
        //获得附件实体类中信息
        ExtCproduct oldExtCproduct = extCproductService.findById(id);
        //获得合同实体类信息
        Contract oldContract = contractService.findById(contractId);
        oldContract.setExtNum(oldContract.getExtNum() - 1);
        oldContract.setTotalAmount(oldContract.getTotalAmount() - oldExtCproduct.getPrice() * oldExtCproduct.getCnumber());
        contractService.update(oldContract);
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId=" +
                contractId + "&contractProductId=" + contractProductId;
    }
}
